# From Greblus: https://blog.greblus.net/2014/11/18/msys2mingw-w64-cython-i-ipython-notebook/

_realname=zeromq
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.5
pkgrel=3
pkgdesc="Fast messaging system built on sockets, C and C++ bindings. aka 0MQ, ZMQ (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.zeromq.org/"
msys2_repository_url="https://github.com/zeromq/libzmq/"
msys2_references=(
  "cpe: cpe:/a:zeromq:libzmq"
  "cpe: cpe:/a:zeromq:zeromq"
)
license=("spdx:MPL-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-libsodium"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-autotools")
optdepends=("${MINGW_PACKAGE_PREFIX}-cppzmq: C++ binding for libzmq")
source=("https://github.com/zeromq/libzmq/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        https://github.com/zeromq/libzmq/commit/a02cbd0646175a44edb1d636911eb8dae12ec13f.patch)
sha256sums=('6653ef5910f17954861fe72332e68b03ca6e4d9c7160eb3a8de5a5a913bfab43'
            '6c5c612b06f4209754991bd51ca42efbfbc4c9db5b86080b0ce3bc179d2e7d3b')

prepare() {
  cd ${_realname}-${pkgver}

  # aarch64 error: use of undeclared identifier 'nsecs_per_usec'
  patch -R -p1 -i "${srcdir}/a02cbd0646175a44edb1d636911eb8dae12ec13f.patch"

  autoreconf -fi
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # CXXFLAGS+=" -DZMQ_HAVE_WINDOWS_TARGET_XP" - for next version
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-libsodium \
    --enable-shared \
    --enable-static \
    --disable-Werror

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  # Fix .pc file
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libzmq.pc
}
